package vip.xiaonuo.core.util;

import cn.hutool.core.codec.Base64;
import lombok.extern.slf4j.Slf4j;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeDriverService;
import org.openqa.selenium.chrome.ChromeOptions;

import java.io.File;
import java.io.FileInputStream;
import java.util.concurrent.TimeUnit;

@Slf4j
public class DriveUtils {
    public static String imgUntil(String url){
        ChromeDriver driver = null;
        FileInputStream inputFile = null;
        ChromeDriverService service = null;
        try {
            //(推荐)，利用ChromeDriverService启动
            //这里"/data/server/legal/chromedriver"是下载的驱动路径，Windows对应chromedriver.exe Linux对应chromedriver，具体路径看你把驱动放在哪
            service = new ChromeDriverService.Builder().usingDriverExecutable(new File("/www/wwwroot/jars/chromedriver")).usingAnyFreePort().build();
            service.start();
            ChromeOptions options = new ChromeOptions();
            //ssl证书支持
            options.setCapability("acceptSslCerts", true);
            //截屏支持
            options.setCapability("takesScreenshot", true);
            //css搜索支持
            options.setCapability("cssSelectorsEnabled", true);
            //设置浏览器参数
            options.addArguments("--headless");
            options.addArguments("--no-sandbox");
            options.addArguments("--disable-gpu");
            options.addArguments("--disable-dev-shm-usage");
            options.setHeadless(true);
            driver = new ChromeDriver(service,options);
            //设置超时，避免有些内容加载过慢导致截不到图
            driver.manage().timeouts().pageLoadTimeout(1, TimeUnit.MINUTES);
            driver.manage().timeouts().implicitlyWait(1, TimeUnit.MINUTES);
            driver.manage().timeouts().setScriptTimeout(1, TimeUnit.MINUTES);
            //设置需要访问的地址
            driver.get(url);
            Thread.sleep(2000);
            //获取高度和宽度一定要在设置URL之后，不然会导致获取不到页面真实的宽高；
            Long width = (Long)driver.executeScript("return document.documentElement.scrollWidth");
            Long height =(Long) driver.executeScript("return document.body.parentNode.scrollHeight");
            System.out.println("高度："+height);
            /*//这里按照网页需求有些是滑动的时候才加在的，如ajax的异步加载
            long temp_height = 0;
            while (true) {
                //每次滚动500个像素，因为懒加载所以每次等待2S 具体时间可以根据具体业务场景去设置
                driver.executeScript("window.scrollBy(0,500)");
                temp_height += 500;
                if(temp_height>=height){
                    break;
                }
            }*/
            //设置窗口宽高，设置后才能截全
            driver.manage().window().setSize(new Dimension(width.intValue(), height.intValue()));
            File srcFile = driver.getScreenshotAs(OutputType.FILE);
            // 这里的需求是将图片转为Base64码后换回，实际按需求控制
            inputFile = new FileInputStream(srcFile);
            byte[] buffer = new byte[(int)srcFile.length()];
            inputFile.read(buffer);

            String content = Base64.encode(buffer);
            return content;

            //设置截图文件保存的路径
            //String screenshotPath = "C:\\wenjian\\YA\\imgGG1.png";
            //FileUtils.copyFile(srcFile, new File(screenshotPath));
        }catch (Exception e){
            e.printStackTrace();
            log.error("截图失败:"+e);
            return null;
        }finally {
            try{
                if (inputFile != null){
                    inputFile.close();
                }
            }catch (Exception ee){

            }
            if (driver!= null){
                driver.quit();
                service.stop();
            }
        }
    }
}
